<?php

require_once "Str.php";

class FormBuilder
{
	var $form;
	var $scheme;
	var $db;
	var $myId;
	var $settings;

	//just give the form object in, so you can set it up 
	function FormBuilder($fullFormObj, $scheme, $myId, $db=Null, $tablePrefix='', $settings=array())
	//outside anyhow you wish 
	//we need db only if there are any selects so we need to query values
	{
		$this->form = $fullFormObj;
		$this->scheme = $scheme;
		$this->db = $db;
		$this->myId = $myId;
		$this->tablePrefix = $tablePrefix;
		$this->settings = $settings;
	}

	function show($action='', $method='post')
	//if editing set changeId otherwise adding
	{
		$out = '';
		if (isset($this->settings['formParams'])) 
		{
			$this->form->formParams = $this->settings['formParams'];
		}
		$out .= $this->form->startForm($action, '', $method);
		$out .= $this->form->startFieldset();
		$changeId = isset($_GET[$this->myId.'_change']) ? $_GET[$this->myId.'_change'] : '';
		
		if ($changeId)
		{
			$this->form->applyDefaults(DBs::selectRow($this->db, $this->myId, "*", "id = $changeId"));
			$submit = "action_{$this->myId}_change"; //TODO - make it settable
			$submitStr  = 'Spremeni navodilo';
		}
		else
		{
			$submit = "action_{$this->myId}_add";
			$submitStr  = 'Dodaj navodilo';		
		}
		
		//few rules ... 
		//id is primary key
		//id_some_table_name is foreign key
		
		foreach($this->scheme as $e)
		{
			$label = isset($e['label']) ? $e['label'] : $e['Field'];
			
			$value = isset($e['forceValue']) ? $e['forceValue'] : $e['Default'];
		
			if (isset($e['forceType']))
			{
				if ($e['forceType'] == 'hidden')
				{
					$out .= $this->form->hidden($e['Field'], $value);
				}
				else
				{
					$out .= $this->form->input($e['forceType'], $e['Field'], $value, '', array('text' => $label));
				}
			}
			else if ($e['Key'] == 'PRI')
			//we have a primary key
			{
				if ($e['Extra'] == 'auto_increment')
				//if auto increment we don't show it
				{
					$out .= $this->form->hidden($e['Field'], $changeId);
				}
				else
				{
					$out .= $this->form->input('text', $e['Field'], '', '', array('text' => $label)); //todo make that labels can be set up
				}
			}
			else if (Str::startsWith($e['Field'], 'id_'))
			//foreign key in some other table
			//so we show select field with id's as values and 
			//what is described in 'Labels' as labels
			//in that table key must be id
			{
				$labelRow = isset($e['SelectLabelRow']) ? ','.$e['SelectLabelRow'] : '';
				$where = isset($e['Filter']) ? $e['Filter'] : '';
				$options = DBs::selectRows($this->db, $this->tablePrefix.substr($e['Field'], 3), "id $labelRow", $where,'',$e['SelectLabelRow'],'','',DB_FETCHMODE_ORDERED, isset($e['SelectLabelRow']));
				if (!isset($e['addEmptyOption'])) $e['addEmptyOption'] = false;
				
				if (true) // todo make optional ---- make order by label optional also
				{
				
					if ($e['addEmptyOption'])
					{
						$options[0] = "";
					}
					foreach ($options as $key => $option)
					{
						if ($key != 0) $options[$key] = "$option [$key]";
					}
				}
				$out .= $this->form->input('select', $e['Field'], '', '', array('text' => $label), array('options' => $options, 'useKeys' => isset($e['SelectLabelRow'])));
			}
			else 
			//we have an ordinary field
			{
				if (Str::startsWith($e['Type'], 'text'))
				{
					$out .= $this->form->input('textarea', $e['Field'], '', '', array('text' => $label));
				}
				else
				{
					$out .= $this->form->input('text', $e['Field'], '', '', array('text' => $label));
				}
			}
		}
		$out .= $this->form->input('submit', $submit, $submitStr, '', array('text'=>'&nbsp;'));
		if (!isset($this->settings['cancelLink'])) $this->settings['cancelLink'] = false;
		
		if ($this->settings['cancelLink']) $link = $this->settings['cancelLink'];
		else $link = $_SERVER['PHP_SELF'];
		$out .= "<dt>&nbsp;</dt><dd><p><a href='{$link}'>&laquo; Prekliči</a></p></dd>";
		$out .= $this->form->endFieldset();
		$out .= $this->form->endForm();
		return $out;			
	}	
}
?>